package com.ruoyi.report_hntjg.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.report_hntjg.domain.ReportHntjgGjbhcbgBaDetail;
import com.ruoyi.report_hntjg.domain.ReportHntjgGjbhcjlbBaDetail;
import com.ruoyi.report_hntjg.domain.ReportHntjgGjbhcjlbBsInfo;
import com.ruoyi.report_hntjg.domain.bo.ReportHntjgGjbhcjlbBsInfoBo;
import com.ruoyi.report_hntjg.domain.vo.ReportHntjgGjbhcjlbBaDetailVo;
import com.ruoyi.report_hntjg.domain.vo.ReportHntjgGjbhcjlbBsInfoVo;
import com.ruoyi.report_hntjg.mapper.ReportHntjgGjbhcjlbBaDetailMapper;
import com.ruoyi.report_hntjg.mapper.ReportHntjgGjbhcjlbBsInfoMapper;
import com.ruoyi.report_hntjg.service.IReportHntjgGjbhcjlbBsInfoService;
import com.ruoyi.report_tg.domain.TdczbhslBaDetail;
import com.ruoyi.report_tg.domain.vo.TdczbhslBaDetailVo;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 钢筋位置及保护层厚度试验检测记录Service业务层处理
 *
 * @author tll
 * @date 2024-08-07
 */
@RequiredArgsConstructor
@Service
public class ReportHntjgGjbhcjlbBsInfoServiceImpl implements IReportHntjgGjbhcjlbBsInfoService {

    private final ReportHntjgGjbhcjlbBsInfoMapper baseMapper;
    private final ReportHntjgGjbhcjlbBaDetailMapper detailMapper;

    /**
     * 查询钢筋位置及保护层厚度试验检测记录
     */
    @Override
    @FIllBasicInfo
    public ReportHntjgGjbhcjlbBsInfoVo queryById(Long excelId) {
        // 主表
        ReportHntjgGjbhcjlbBsInfoVo vo = baseMapper.selectVoById(excelId);
        // 子表
        // 查询detail
        List<ReportHntjgGjbhcjlbBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<ReportHntjgGjbhcjlbBaDetail>()
            .eq(ReportHntjgGjbhcjlbBaDetail::getExcelId, excelId)
            .orderByAsc(ReportHntjgGjbhcjlbBaDetail::getSort));
        if (CollUtil.isNotEmpty(baDetailVos)) {
            vo.setBaohcList(baDetailVos.subList(0,40));
            vo.setGangjList(baDetailVos.subList(40,69));
        }
//        LambdaQueryWrapper<ReportHntjgGjbhcjlbBaDetail> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(excelId != null, ReportHntjgGjbhcjlbBaDetail::getExcelId, excelId);
//        lqw.orderByAsc(ReportHntjgGjbhcjlbBaDetail::getCreateTime);
//        lqw.orderByAsc(ReportHntjgGjbhcjlbBaDetail::getSort);
//        List<ReportHntjgGjbhcjlbBaDetailVo> detailList = detailMapper.selectVoList(lqw);
////        vo.setDetailList(detailList);
//        // 保护层厚度、钢筋间距列表赋值
//        List<ReportHntjgGjbhcjlbBaDetailVo> baohcList = new ArrayList<>();
//        List<ReportHntjgGjbhcjlbBaDetailVo> gangjList = new ArrayList<>();
//        for (ReportHntjgGjbhcjlbBaDetailVo item : detailList) {
//            switch (item.getType()) {
//                case "baohc":
//                    baohcList.add(item);
//                    break;
//                case "gangj":
//                    gangjList.add(item);
//                    break;
//            }
//        }
//        vo.setBaohcList(baohcList);
//        vo.setGangjList(gangjList);
        return vo;
    }

    /**
     * 查询钢筋位置及保护层厚度试验检测记录列表
     */
    @Override
    public TableDataInfo<ReportHntjgGjbhcjlbBsInfoVo> queryPageList(ReportHntjgGjbhcjlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportHntjgGjbhcjlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportHntjgGjbhcjlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询钢筋位置及保护层厚度试验检测记录列表
     */
    @Override
    public List<ReportHntjgGjbhcjlbBsInfoVo> queryList(ReportHntjgGjbhcjlbBsInfoBo bo) {
        LambdaQueryWrapper<ReportHntjgGjbhcjlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportHntjgGjbhcjlbBsInfo> buildQueryWrapper(ReportHntjgGjbhcjlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportHntjgGjbhcjlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportHntjgGjbhcjlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportHntjgGjbhcjlbBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportHntjgGjbhcjlbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportHntjgGjbhcjlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportHntjgGjbhcjlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportHntjgGjbhcjlbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportHntjgGjbhcjlbBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportHntjgGjbhcjlbBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportHntjgGjbhcjlbBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(bo.getYangpsj() != null, ReportHntjgGjbhcjlbBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportHntjgGjbhcjlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportHntjgGjbhcjlbBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportHntjgGjbhcjlbBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportHntjgGjbhcjlbBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportHntjgGjbhcjlbBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportHntjgGjbhcjlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportHntjgGjbhcjlbBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobmc()), ReportHntjgGjbhcjlbBsInfo::getBaobmc, bo.getBaobmc());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobbh()), ReportHntjgGjbhcjlbBsInfo::getBaobbh, bo.getBaobbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportHntjgGjbhcjlbBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancr()), ReportHntjgGjbhcjlbBsInfo::getJiancr, bo.getJiancr());
        lqw.eq(StringUtils.isNotBlank(bo.getJilr()), ReportHntjgGjbhcjlbBsInfo::getJilr, bo.getJilr());
        lqw.eq(StringUtils.isNotBlank(bo.getFuhr()), ReportHntjgGjbhcjlbBsInfo::getFuhr, bo.getFuhr());
        lqw.eq(bo.getRiq() != null, ReportHntjgGjbhcjlbBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportHntjgGjbhcjlbBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportHntjgGjbhcjlbBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportHntjgGjbhcjlbBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(bo.getBaohcSjz() != null, ReportHntjgGjbhcjlbBsInfo::getBaohcSjz, bo.getBaohcSjz());
        lqw.eq(bo.getGangjSjz() != null, ReportHntjgGjbhcjlbBsInfo::getGangjSjz, bo.getGangjSjz());
        lqw.eq(bo.getBaohcYxpc() != null, ReportHntjgGjbhcjlbBsInfo::getBaohcYxpc, bo.getBaohcYxpc());
        lqw.eq(bo.getGangjYxpc() != null, ReportHntjgGjbhcjlbBsInfo::getGangjYxpc, bo.getGangjYxpc());
        return lqw;
    }

    /**
     * 新增钢筋位置及保护层厚度试验检测记录
     */
    @Override
    public Boolean insertByBo(ReportHntjgGjbhcjlbBsInfoBo bo) {
        ReportHntjgGjbhcjlbBsInfo add = BeanUtil.toBean(bo, ReportHntjgGjbhcjlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            ArrayList<ReportHntjgGjbhcjlbBaDetail> bhcList = new ArrayList<>(40);
            for (long i = 1; i <= 40L; i++) {
                bhcList.add(new ReportHntjgGjbhcjlbBaDetail(add.getExcelId(), i));
            }
            detailMapper.insertBatch(bhcList);

            ArrayList<ReportHntjgGjbhcjlbBaDetail> gjList = new ArrayList<>(30);
            for (long i = 41; i <= 70L; i++) {
                gjList.add(new ReportHntjgGjbhcjlbBaDetail(add.getExcelId(), i));
            }
            detailMapper.insertBatch(gjList);
        }
        return flag;
    }

    /**
     * 修改钢筋位置及保护层厚度试验检测记录
     */
    @Override
    public Boolean updateByBo(ReportHntjgGjbhcjlbBsInfoBo bo) {
        // 主表
        ReportHntjgGjbhcjlbBsInfo update = BeanUtil.toBean(bo, ReportHntjgGjbhcjlbBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaohcList(), ReportHntjgGjbhcjlbBaDetail.class));
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getGangjList(), ReportHntjgGjbhcjlbBaDetail.class));
        }
        return flag;

//        boolean upd = baseMapper.updateById(update) > 0;
//        // 子表
//        boolean upd2 = false;
//        boolean insert = false;
//        List<ReportHntjgGjbhcjlbBaDetail> detailList = new ArrayList();
//        detailList.addAll(bo.getBaohcList());
//        detailList.addAll(bo.getGangjList());
//        if (ObjectUtils.isNotEmpty(detailList)) {
//            // 根据excelId查询明细
//            QueryWrapper<ReportHntjgGjbhcjlbBaDetail> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("excel_id", bo.getExcelId());
//            List<ReportHntjgGjbhcjlbBaDetail> list = reportHntjgGjbhcjlbBaDetailMapper.selectList(queryWrapper);
//            for (ReportHntjgGjbhcjlbBaDetail det : detailList) {
//                if (ObjectUtils.isEmpty(list)) {
//                    // 新增
//                    insert = reportHntjgGjbhcjlbBaDetailMapper.insert(det) > 0;
//                } else if (ObjectUtils.isNotEmpty(det.getDetailId())) {
//                    // 修改
//                    upd2 = reportHntjgGjbhcjlbBaDetailMapper.updateById(det) > 0;
//                } else {
//                    throw new ServiceException("更新主键不能为空, 请刷新后重试!");
//                }
//            }
//        }
//        return upd && upd2 && insert;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportHntjgGjbhcjlbBsInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除钢筋位置及保护层厚度试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
